package com.sicheng.lc.周赛.分类.哈西表or计数or滑动窗口or双指针;

import java.util.HashMap;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/24 10:37
 */
public class 相等行列对 {
    //https://leetcode.cn/problems/equal-row-and-column-pairs/
    public int equalPairs(int[][] grid) {
        int n = grid.length;
        HashMap<String, Integer> map = new HashMap<>(n << 2);
        StringBuilder path = new StringBuilder();
        for (int[] line : grid) {
            path.delete(0, path.length());
            for (int i : line) {
                path.append(i).append(' ');
            }
            String key = path.toString();
            map.put(key, map.getOrDefault(key, 0) + 1);
        }

        int res = 0;
        for (int j = 0; j < n; j++) {
            path.delete(0, path.length());
            for (int[] nums : grid) {
                path.append(nums[j]).append(' ');
            }
            String key = path.toString();
            res += map.getOrDefault(key, 0);
        }
        return res;
    }
}
